package com.happy.second.Config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {"com.happy.second.Mapper.YnnMapper"},sqlSessionFactoryRef = "sqlSessionFactoryYnn")
public class ynnDataConfig {

    @Autowired
    @Qualifier("ynnData")
    private DataSource ynnDataSource;

    @Bean(name = "sqlSessionFactoryYnn")
    @Primary
    public SqlSessionFactory sqlSessionFactoryYnn() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/ynnmapper/*.xml"));
        factoryBean.setDataSource(ynnDataSource);
        return factoryBean.getObject();
    }

    @Bean(name = "sqlSessionFactoryYnn")
    @Primary
    public SqlSessionTemplate sqlSessionTemplateYnn() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryYnn());
        return template;
    }
}
